package com.zkhz.base.controller;

import cn.hutool.core.codec.Base64;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zkhz.base.common.dto.PageResultDTO;
import com.zkhz.base.common.dto.ResponseDTO;
import com.zkhz.base.constant.enums.ErrorStatus;
import com.zkhz.base.domian.dto.MyUserDTO;
import com.zkhz.base.domian.entity.MyUser;
import com.zkhz.base.domian.mapper.MyUserDTOMapper;
import com.zkhz.base.domian.vo.MyUserPageVO;
import com.zkhz.base.exception.BusinessException;
import com.zkhz.base.mapper.MyUserMapper;
import com.zkhz.base.security.util.MyPasswordEncoder;
import com.zkhz.base.service.MyUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@Slf4j
//@CrossOrigin
@Api(tags = "测试接口")
@RequestMapping(value = "/myUser")
@RestController
public class MyUserController {
    @Autowired
    private MyUserService myUserService;
    @Autowired
    private MyUserMapper myUserMapper;
    @Autowired
    private MyPasswordEncoder myPasswordEncoder;

    @ApiOperation(value = "测试mybatis plus", notes = "环境")
    @GetMapping
    public ResponseDTO<String> test(@ApiParam(value = "参数1", required = true) String one,
                                   @ApiParam(value = "参数2") String two) {
        log.info(one);
        log.error(two);
        if (StringUtils.isNotEmpty(two)) {
            throw new BusinessException(ErrorStatus.S0300, "发生异常");
        }
        MyUser u = new MyUser();
        u.setUsername("liaozhiyong");
        u.setPassword(myPasswordEncoder.encode("123456"));
        u.setSchrodingerCat(Base64.encode("123456"));
        u.setName("廖智勇");
        u.setTitle("孙悟空");
        u.setAge(18);
        u.setCreateUser("我");
        u.setUpdateUser("还是我");
        myUserMapper.insert(u);
        return ResponseDTO.ok(u.toString());
    }

    @ApiOperation(value = "新增用户", notes = "单一接口")
    @PostMapping(value = "/create")
    public ResponseDTO<MyUserDTO> create(@ApiParam(value = "新增的用户信息") @RequestBody MyUserDTO myUserDTO) {
        MyUser myUser = MyUserDTOMapper.toMyUser(myUserDTO);
        myUser.setSchrodingerCat(Base64.encode(myUser.getPassword()));
        myUser.setPassword(myPasswordEncoder.encode(myUser.getPassword()));
        myUserMapper.insert(myUser);
        return ResponseDTO.ok(MyUserDTOMapper.toMyUserDTO(myUser));
    }

    @ApiOperation(value = "查询用户", notes = "单一接口")
    @GetMapping(value = "/{id}")
    public ResponseDTO<MyUserDTO> load(@ApiParam(value = "用户id") @PathVariable(value = "id") String id) {
        MyUser myUser = myUserMapper.selectById(id);
        if (myUser == null) {
            return ResponseDTO.fail(ErrorStatus.S0302.getDetail());
        }
        return ResponseDTO.ok(MyUserDTOMapper.toMyUserDTO(myUser));
    }

    @ApiOperation(value = "更新用户", notes = "单一接口")
    @PutMapping(value = "/{id}")
    public ResponseDTO<MyUserDTO> update(@ApiParam(value = "用户id") @PathVariable(value = "id") String id,
                                         @ApiParam(value = "用户信息") @RequestBody MyUserDTO myUserDTO) {
        MyUser myUser = myUserMapper.selectById(id);
        if (myUser == null) {
            return ResponseDTO.fail(ErrorStatus.S0302.getDetail());
        }
        MyUserDTOMapper.copyToMyUser(myUserDTO, myUser);
        myUser.setId(id);
        myUser.setSchrodingerCat(Base64.encode(myUser.getPassword()));
        myUser.setPassword(myPasswordEncoder.encode(myUser.getPassword()));
        myUserMapper.updateById(myUser);
        return ResponseDTO.ok(MyUserDTOMapper.toMyUserDTO(myUser));
    }

    @ApiOperation(value = "删除用户", notes = "单一接口")
    @DeleteMapping(value = "/{ids}")
    public ResponseDTO<String> delete(@ApiParam(value = "用户ids,逗号分隔") @PathVariable(value = "ids") String ids) {
        List<String> idList = Arrays.asList(ids.split("[,]"));
        List<MyUser> myUsers = myUserMapper.selectBatchIds(idList);
        if (idList.size() != myUsers.size()) {
            return ResponseDTO.fail(ErrorStatus.S0302.getDetail());
        }
        myUserMapper.deleteBatchIds(idList);
        return ResponseDTO.ok("删除成功");
    }

    @ApiOperation(value = "分页查询用户", notes = "单一接口")
    @PostMapping(value = "/page")
    public ResponseDTO<PageResultDTO<MyUserDTO>> page(@ApiParam(value = "用户id") @RequestBody MyUserPageVO<MyUser> userDTOPage) {
        QueryWrapper<MyUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("username", userDTOPage.getUsername());
        queryWrapper.like("name", userDTOPage.getName());
        queryWrapper.eq("age", userDTOPage.getAge());
        Page<MyUser> userPage = myUserMapper.selectPage(userDTOPage.page(), queryWrapper);

        List<MyUserDTO> myUserDTOS = MyUserDTOMapper.toMyUserDTOList(userPage.getRecords());
        PageResultDTO<MyUserDTO> result = PageResultDTO.of(myUserDTOS);
        result.setPageSize(userPage.getSize());
        result.setPageNum(userPage.getCurrent());
        result.setTotal(userPage.getTotal());
        result.setTotalPages(userPage.getPages());
        return ResponseDTO.ok(result);
    }

}
